﻿using Framework.Core.Consts;
using my.abp.Domain.IManagers.User;
using my.abp.Domain.Repositories.User;
using my.abp.Domain.Shared.Entities;
using System.Linq;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Services;
using Volo.Abp.Uow;

namespace my.abp.Domain.Managers.User
{
	public class SysUserRoleManager : DomainService, ISysUserRoleManager
	{
		private readonly ISysUserRoleRepository _sysUserRoleRep;
		private readonly IDistributedCache<object, string> _sysCacheService;

		public SysUserRoleManager(ISysUserRoleRepository sysUserRoleRep, IDistributedCache<object, string> sysCacheService)
		{
			_sysUserRoleRep = sysUserRoleRep;
			_sysCacheService = sysCacheService;
		}

		/// <summary>
		/// 根据用户Id获取角色集合
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public async Task<List<SysRole>> GetUserRoleList(long userId)
		{
			var queryable = await _sysUserRoleRep.AsQueryable();
			var sysUserRoleList = await queryable.Mapper(u => u.SysRole, u => u.RoleId)
				.Where(u => u.UserId == userId).ToListAsync();
			return sysUserRoleList.Where(u => u.SysRole != null).Select(u => u.SysRole).ToList();
		}

		/// <summary>
		/// 授权用户角色
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		[UnitOfWork]
		public async Task GrantUserRole(long UserId, List<long> RoleIdList)
		{
			await _sysUserRoleRep.DeleteAsync(u => u.UserId == UserId);

			if (RoleIdList == null || RoleIdList.Count < 1) return;
			var roles = RoleIdList.Select(u => new SysUserRole
			{
				UserId = UserId,
				RoleId = u
			}).ToList();
			await _sysUserRoleRep.InsertRangeAsync(roles);
			_sysCacheService.Remove(CacheConst.KeyUserButton + UserId);
		}

		/// <summary>
		/// 根据用户Id删除用户角色
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public async Task DeleteUserRoleByUserId(long userId)
		{
			await _sysUserRoleRep.DeleteAsync(u => u.UserId == userId);
			_sysCacheService.Remove(CacheConst.KeyUserButton + userId);
		}

		/// <summary>
		/// 根据用户Id获取角色Id集合
		/// </summary>
		/// <param name="userId"></param>
		/// <returns></returns>
		public async Task<List<long>> GetUserRoleIdList(long userId)
		{
			//var queryable = await _sysUserRoleRep.AsQueryable();
			//List<SysUserRole> lSysUserRole = await queryable.Where(u => u.UserId == userId).ToListAsync();

			//return lSysUserRole.Select(u => u.RoleId).ToList();
			return await _sysUserRoleRep.GetUserRoleIdList(userId);
		}

		/// <summary>
		/// 根据角色Id获取用户Id集合
		/// </summary>
		/// <param name="roleId"></param>
		/// <returns></returns>
		public async Task<List<long>> GetUserIdList(long roleId)
		{
			var queryable = await _sysUserRoleRep.AsQueryable();
			return await queryable.Where(u => u.RoleId == roleId).Select(u => u.UserId).ToListAsync();
		}

		/// <summary>
		/// 根据角色Id删除用户角色
		/// </summary>
		/// <param name="roleId"></param>
		/// <returns></returns>
		public async Task DeleteUserRoleByRoleId(long roleId)
		{
			var queryable = await _sysUserRoleRep.AsQueryable();

			await queryable
				 .Where(u => u.RoleId == roleId)
				 .Select(u => u.UserId)
				 .ForEachAsync(userId =>
				 {
					 _sysCacheService.Remove(CacheConst.KeyUserButton + userId);
				 });

			await _sysUserRoleRep.DeleteAsync(u => u.RoleId == roleId);
		}
	}
}

